package cn.doitedu.javase.blocking_queue;

import java.util.Scanner;

public class ThreadPoolTest {

    public static void main(String[] args) throws InterruptedException {


        ThreadPool threadPool = new ThreadPool(2);

//        threadPool.submit(new MyTask(1));
//        threadPool.submit(new MyTask(2));
//        threadPool.submit(new MyTask(3));
//        threadPool.submit(new MyTask(4));
//        threadPool.submit(new MyTask(5));
//        threadPool.submit(new MyTask(6));
//        threadPool.submit(new MyTask(7));
//        threadPool.submit(new MyTask(8));

        // 从控制台接收taskId,收到一个taskId，就提交一个任务到线程池
        Scanner scanner = new Scanner(System.in);

        while(true){
            String line = scanner.nextLine();  // 阻塞方法
            int taskId = Integer.parseInt(line);

            threadPool.submit(new MyTask(taskId));
        }

    }



}

class MyTask implements Runnable{
    int taskId;
    public MyTask(int taskId){
        this.taskId = taskId;
    }

    @Override
    public void run() {

        for(int i=0;i<10;i++){
            System.out.println(taskId+" ==> " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
